function [] = mylinear()

fontsize=18;
fontname_num='Times New Roman';
position_fig=[10,10,15,10];
position_axe=[0.15,0.15,0.82,0.82];

x_start=-10;
x_end=10;
y_start=-10;
y_end=10;
num=100;


function [X,Y] = create_data()
  
    theta=[2;3;4];
    x=linspace(x_start,x_end,num);
    y=linspace(y_start,y_end,num);
    [xm,ym]=meshgrid(x,y);
    [zm]=linear_model(theta,xm,ym);
    
    n=50;
    xr=randi([x_start,x_end],n,1);
    yr=randi([y_start,y_end],n,1);
    [zr]=linear_model(theta,xr,yr)+5*randn(n,1);
    
    fig=1;
    self_surf(xm,ym,zm,fig);
    hold on;
    self_plot3(xr,yr,zr,fig);
    
    cone=ones(n,1);
    X=[xr,yr,cone];
    Y=zr;      
end

function [] = self_plot3(X,Y,Z,fig)
    figure(fig);
    plot3(X,Y,Z,'linestyle','none','marker','^','markersize',8,'MarkerFaceColor','g');
    set(gcf,'units','centimeters','position',position_fig);
    set(gca,'position',position_axe);
    set(gca,'fontsize',fontsize,'fontname',fontname_num);
    box on;    
    alpha(0.3);
end

function [] = self_surf(X,Y,Z,fig)
    figure(fig);
    surf(X,Y,Z,'linestyle','none');
    set(gcf,'units','centimeters','position',position_fig);
    set(gca,'position',position_axe);
    set(gca,'fontsize',fontsize,'fontname',fontname_num);
    box on;    
    alpha(0.3);
end
function [z]=linear_model(theta,x,y)
    a=theta(1);
    b=theta(2);
    c=theta(3);
    z=a*x+b*y+c;
end
function []=main()
    [X,Y]=create_data();
    x=linspace(x_start,x_end,num);
    y=linspace(y_start,y_end,num);
    theta=(X'*X)\(X'*Y);
    [xm,ym]=meshgrid(x,y);
    [zm]=linear_model(theta,xm,ym);
    
    xr=X(:,1);
    yr=X(:,2);
    zr=Y;
    fig=2;
    self_surf(xm,ym,zm,fig);
    hold on;
    self_plot3(xr,yr,zr,fig);
end
main();
end

